Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevent zero from being passed to array_chunk() #686

Merged
merged 1 commit into from
Mar 12, 2024

Conversation

GreyWyvern
Copy link
Contributor

@GreyWyvern GreyWyvern commented Mar 6, 2024

Type of pull request

  • Bug fix (involves code and configuration changes)

About

Passing a zero (0) value to the array_chunk() function causes an error, and in rare cases, a PDF XRef object may be added to a document with an "empty" /W [0 0 0] command. In RawDataParser.php this would cause the $rowlen variable to be set to zero and cause an error.

Add a simple check to return an empty array in this case. Resolves #679.

It is very difficult to create a unit test for this error as it requires a PDF to be generated with "empty" sections with the specific /W [0 0 0] command. As the error occurs at a point when the entire document is being considered, we can't just feed PdfParser some test PDF code that's a subsection of a full document. The sample PDF given by the reporter of issue #679 contained personal info that we cannot include in PdfParser and they did not know how to generate a similar file. It is hoped we can merge this PR without a unit test added, since it is, at it's core, just a check for a zero value which should have been in the code already. :)

Thanks to @KeanuTang for the initial analysis and provided code solution, with which I built this PR.

Checklist for code / configuration changes

In case you changed the code/configuration, please read each of the following checkboxes as they contain valuable information:

  • Please add at least one test case (unit test, system test, ...) to demonstrate that the change is working. If existing code was changed, your tests cover these code parts as well.
    Code changes without any tests are likely to be rejected. If you dont know how to write tests, no problem, tell us upfront and we may add them ourselves or discuss other ways.
  • Please run PHP-CS-Fixer before committing, to confirm with our coding styles. See https://github.com/smalot/pdfparser/blob/master/.php-cs-fixer.php for more information about our coding styles.
  • In case you fix an existing issue, please do one of the following:
    • Write in this text something like fixes #1234 to outline that you are providing a fix for the issue #1234.

Passing a zero (0) value to the `array_chunk()` function causes an error, and in rare cases, a PDF XRef object may be added to a document with an "empty" `/W [0 0 0]` command. This would cause the `$rowlen` variable to be set to zero and cause an error.

Add a simple check to return an empty array in this case.
@k00ni k00ni added the fix label Mar 7, 2024
Copy link
Collaborator

@k00ni k00ni left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 @GreyWyvern

About the missing unit test: You are right, its very difficult to write a test for this code part. I checked current code coverage and this part is covered by two other tests indirectly (https://scrutinizer-ci.com/g/smalot/pdfparser/code-structure/master/code-coverage/src/Smalot/PdfParser/RawData/RawDataParser.php). So in this case its appropriate.

@k00ni k00ni merged commit 6c9617c into smalot:master Mar 12, 2024
29 checks passed
@GreyWyvern GreyWyvern deleted the zero-rowlen branch May 10, 2024 15:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Bug in RawDataParser.php when a row has no data
2 participants